<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>排放因子管理</title>
    <jsp:include page="../inc/css.jsp"/>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <button type="button" title="新增排放因子" class="btn btn-success" onclick="_emissionCodeList.fn.addInfo()">新增排放因子</button>

                            <div class="form-inline" style="margin-top: 10px;">
                                <div class="form-group">
                                    <select id="searchTypeList" class="form-control" style="margin-right: 10px;">
                                        <option value="">全部类型</option>
                                        <option value="1">废气</option>
                                        <option value="2">废水</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <input id="searchKeyWord" placeholder="关键词查询" class="form-control"/>
                                </div>

                                <button type="button" title="查询" class="btn btn-info" style="margin-left: 10px;" onclick="_emissionCodeList.fn.reloadData()">
                                    查询
                                </button>
                                <button type="button" title="重置搜索条件并查询" class="btn btn-success" style="margin-left: 10px;" onclick="_emissionCodeList.fn.resetAndReloadData()">
                                    重置
                                </button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>类型</th>
                                        <th>名称</th>
                                        <th style="width: 80px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<div class="modal inModal fade" id="emissionCodeModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">排放因子详情</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form role="form" class="form-group">
                    <input type="hidden" id="emissionCodeId"/>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">类型：</label>

                        <div class="col-9">
                            <select id="typeList" class="form-control" style="width: 95%;">
                                <option value="1">废气</option>
                                <option value="2">废水</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-inline" style="margin-bottom: 10px;">
                        <label class="col-3">名称：</label>

                        <div class="col-9">
                            <input type="text" id="updateContent" maxlength="30" class="form-control" style="width: 95%"/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="_emissionCodeList.fn.saveInfo()">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- page script -->
<script type="text/javascript">
    let _emissionCodeList = {
        v: {
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                _emissionCodeList.fn.dataTableInit();
            },
            reloadData: function () {
                _emissionCodeList.v.list = [];
                _emissionCodeList.v.dTable.DataTable().ajax.reload(null, false);
            },
            resetAndReloadData: function () {
                $('#searchCompanyList').val('');
                $('#searchKeyWord').val('');

                _emissionCodeList.fn.reloadData();
            },
            dataTableInit: function () {
                _emissionCodeList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许排放因子改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/emissionCode/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {
                            "data": "type",
                            render: function (data) {
                                if (data === 1) {
                                    return '废气';
                                } else {
                                    return '废水';
                                }
                            }
                        },
                        {
                            "data": "content",
                            render: function (data) {
                                return formatStr(data, 10);
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='编辑' class='btn btn-primary btn-sm edit'><i class='fa fa-edit'></i></button>" +
                                "&nbsp;&nbsp;" +
                                "<button type='button' title='删除' class='btn btn-danger btn-sm delete'><i class='fa fa-remove'></i></button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _emissionCodeList.v.list.push(data);
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        $('td', row).last().find(".edit").click(function () {
                            _emissionCodeList.fn.editInfo(data);
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _emissionCodeList.fn.delInfo(data.id);
                        });
                    },
                    /*带参数*/
                    "fnServerParams": function (aoData) {
                        _emissionCodeList.v.list = [];

                        aoData.type = $('#searchTypeList').val();
                        aoData.keyWord = $('#searchKeyWord').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            saveInfo: function () {
                let content = $('#updateContent').val();

                if (content === '') {
                    info('请输入排放因子名称', "error");
                    return;
                }

                startLoading();

                $.post('backend/emissionCode/save', {
                    id: $('#emissionCodeId').val(),
                    type: $('#typeList').val(),
                    content: content
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        $("#emissionCodeModal").modal("hide");

                        _emissionCodeList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            },
            addInfo: function () {
                $('#emissionCodeId').val('');
                $('#typeList').val(1);
                $('#updateContent').val('');

                $("#emissionCodeModal").modal("show");
            },
            editInfo: function (data) {
                $('#emissionCodeId').val(data.id);
                $('#typeList').val(data.type);
                $('#updateContent').val(data.content);

                $("#emissionCodeModal").modal("show");
            },
            delInfo: function (id) {
                swal({
                    title: "您确定要删除这条记录吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的，删除！",
                    cancelButtonText: "不，取消",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        startLoading();

                        $.post('backend/emissionCode/del', {
                            id: id
                        }, function (result) {
                            endLoading();

                            if (result.code === 0) {
                                swal("删除成功！", "您已经删除了这条记录。", "success");

                                _emissionCodeList.fn.reloadData();
                            } else {
                                info(result.msg, "error");
                            }
                        });
                    } else {
                        swal("已取消", "您取消了删除操作！", "error");
                    }
                })
            }
        }
    };

    $(document).ready(function () {
        _emissionCodeList.fn.init();

        window.onload = function () {
            checkSelectMenu("emissionCodeLi");

            loadMenuColor('emissionCodeLi');
        }
    });
</script>
</body>
</html>